<template>
  <div class="navbar">
    <ul>
      <li
        v-for="(item,index) in list"
        :key="index"
        @click="itemClick(index)"
        :class="{active: isActive == urlList[index]}"
      >
        {{item}}
        <span :class="{click: isActive == urlList[index]}"></span>
      </li>
      <span class="hot">HOT</span>
    </ul>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      isActive: 'discover',
      urlList: ['discover', 'my', 'friend', 'store', 'nmusician', 'download']
    }
  },
  methods: {
    // 鼠标点击改变样式
    itemClick(index) {
      this.isActive = this.urlList[index]
      this.$router.push('/' + this.urlList[index])
    }
  },
  created() {
    // 修复bug,刷新页面时，isActive会被重置
    // 修复bug,首次打开window.location.pathname为空
    if (window.location.pathname.split('/')[1]) {
      this.isActive = window.location.pathname.split('/')[1]
    }
  }
}
</script>

<style scoped>
.navbar {
  height: 100%;
}

ul {
  height: 100%;
  cursor: pointer;
}

li {
  position: relative;
  float: left;

  display: flex;
  align-items: center;

  height: 70px;
  padding: 0 19px;
  font-size: 14px;
  color: #ccc;
}

li span {
  position: absolute;
  visibility: hidden;

  bottom: 0;
  left: 50%;
  margin-left: -7px;
  width: 14px;
  height: 14px;

  border-bottom: 7px solid #c20c0c;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 7px solid transparent;
}

.click {
  visibility: visible;
}

.active,
li:hover {
  color: #fff;
  background-color: #000;
}

ul {
  position: relative;
}

.hot {
  position: absolute;

  border-radius: 10px;
  font-size: 10px;
  background-color: #c20c0c;
  color: #fff;
  padding: 1px 5px;
  right: -15px;
  top: 10px;
}
</style>